﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication.Code;
using Company.Merchandising.DataAccessLayer.DataAccessCommon;
using Telerik.Web.UI;
using System.Drawing;
using WebApplication.Controls.Management.Dictionaries;
using Company.Merchandising.BusinessLayer.DataTransferObjects;
using Company.Merchandising.ServiceLayer.EntityManagement;

namespace WebApplication.Restricted.Management.Dictionaries
{
	public partial class ProvincesManagement : EntityPage
	{
		protected void ProvincesGrid_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			ProvinceEditControl provincesControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as ProvinceEditControl;
			if (provincesControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			ProvinceManager provinceManager = new ProvinceManager(ModelEntities);
			try
			{
				provinceManager.AddProvince(provincesControl.EditedProvince);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		protected void ProvincesGrid_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			if (editedItem["Id"] == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			int editedProvinceId;
			if (!int.TryParse(editedItem["Id"].Text, out editedProvinceId))
			{
				AddErrorMessage("Bląd wewnętrzny");
				e.Canceled = true;
				return;
			}

			ProvinceManager provinceManager = new ProvinceManager(ModelEntities);
			try
			{
				provinceManager.DeleteProvince(editedProvinceId);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		protected void ProvincesGrid_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			if (editedItem["Id"] == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			int editedProvinceId;
			if (!int.TryParse(editedItem["Id"].Text, out editedProvinceId))
			{
				AddErrorMessage("Bląd wewnętrzny");
				e.Canceled = true;
				return;
			}

			ProvinceEditControl provincesControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as ProvinceEditControl;
			if (provincesControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}
			DTOProvince dtoProvince = provincesControl.EditedProvince;
			dtoProvince.Id = editedProvinceId;
			ProvinceManager provinceManager = new ProvinceManager(ModelEntities);
			try
			{
				provinceManager.UpdateProvince(dtoProvince);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		private void AddErrorMessage(string errorMessage)
		{
			Label errorLabel = new Label();
			errorLabel.Text = errorMessage;
			errorLabel.ForeColor = Color.Red;

			ProvincesGrid.Controls.Add(errorLabel);
		}

		protected void EntityDataSource1_Selecting(object sender, EntityDataSourceSelectingEventArgs e)
		{
		}
	}
}
